/**
 * Created by pkd on 2017/5/24.
 */

var globalVar = module.exports;
var configDao = require('./dao/config.js');
var appDao = require('./dao/app_admin');


function initGlobalVar() {

    var AliDaYuClient = require('./libs/alidayu/topClient').TopClient;

    globalVar.apiSms = new AliDaYuClient({
        'appkey': '23708727',
        'appsecret': '39459d33f1055cad5f83f1aa76b7994a',
        'REST_URL': 'http://gw.api.taobao.com/router/rest'
    });

    configDao.getConfigs(0,function (error, result) {
        if(error){
            console.log(error);
            return;
        }
        result.forEach(function (pair) {
            var data = {};
            data.value = pair.value;
            data.comment = pair.comment;
            globalVar.config[pair.app_sid+'_'+pair.key] = data;
        })
        console.log(globalVar.config);
    });

    appDao.getAppKeysAndSecrets(null,function (err,result) {
        if(err){
            console.log('获取app key和secret失败，请检查或手动获取');
            return;
        }
        result.forEach(function (app) {
            globalVar.apps[app.sid] = app.secret;
        })
    });

}

globalVar.config = {};
globalVar.apps = {};
initGlobalVar();
